﻿using AutoMapper;
using FluentValidation;
using Mes.Application.Common;
using Microsoft.AspNetCore.Mvc;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.Services.MasterData;

namespace Mes.Application.Web.Controllers.MasterData
{
    [ApiController]
    [Route("/api/v1")]
    public class ProductionUnitTypeController : ControllerBase
    {
        private readonly ILogger<ProductionUnitTypeController> _logger;
        private readonly IProductionUnitTypeService _productionUnitTypeService;
        private readonly IMapper _mapper;
        private readonly IValidator<ProductionUnitTypeInput> _productionUnitTypeInputValidator;
        private readonly IValidator<ProductionUnitTypeUpdateInput> _productionUnitTypeUpdateInputValidator;
        private readonly IValidator<ProductionUnitTypePageInput> _productionUnitTypePageInputValidator;

        public ProductionUnitTypeController(ILogger<ProductionUnitTypeController> logger,
            IProductionUnitTypeService productionUnitTypeService,
            IMapper mapper,
            IValidator<ProductionUnitTypeInput> productionUnitTypeInputValidator,
            IValidator<ProductionUnitTypeUpdateInput> productionUnitTypeUpdateInputValidator,
            IValidator<ProductionUnitTypePageInput> productionUnitTypePageInputValidator
            )
        {
            _logger = logger;
            _productionUnitTypeService = productionUnitTypeService;
            _mapper = mapper;
            _productionUnitTypeInputValidator = productionUnitTypeInputValidator;
            _productionUnitTypeUpdateInputValidator = productionUnitTypeUpdateInputValidator;
            _productionUnitTypePageInputValidator = productionUnitTypePageInputValidator;
        }

        /// <summary>
        /// 新增生产单元类型
        /// </summary>
        /// <param name="productionUnitTypeInput"></param>
        /// <returns></returns>
        [HttpPost("productionunittypes")]
        public async Task<ActionResult> AddProductionUnitType(ProductionUnitTypeInput productionUnitTypeInput)
        {
            try
            {
                var input = await _productionUnitTypeInputValidator.ValidateAsync(productionUnitTypeInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var item=await _productionUnitTypeService.AddProductionUnitTypeAsync(productionUnitTypeInput);
                return Ok(item);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 修改生产单元类型
        /// </summary>
        /// <param name="productionUnitTypeUpdateInput"></param>
        /// <returns></returns>
        [HttpPut("productionunittypes")]
        public async Task<ActionResult> UpdateProductionUnitType(ProductionUnitTypeUpdateInput productionUnitTypeUpdateInput)
        {
            try
            {
                var input = await _productionUnitTypeUpdateInputValidator.ValidateAsync(productionUnitTypeUpdateInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _productionUnitTypeService.UpdateProductionUnitTypeAsync(productionUnitTypeUpdateInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除生产单元类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("productionunittypes")]
        public async Task<ActionResult> DeleteProductionUnitType(int[] ids)
        {
            try
            {
                await _productionUnitTypeService.DeleteProductionUnitTypeAsync(ids);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询生产单元类型
        /// </summary>
        /// <param name="productionUnitTypePageInput"></param>
        /// <returns>PagedItemsData<ProductionUnitTypeResult></returns>
        [HttpGet("productionunittypes")]
        public async Task<ActionResult> GetProductionUnitTypes([FromQuery] ProductionUnitTypePageInput productionUnitTypePageInput)
        {
            try
            {
                var input = await _productionUnitTypePageInputValidator.ValidateAsync(productionUnitTypePageInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var result = await _productionUnitTypeService.GetProductionUnitTypesAsync(productionUnitTypePageInput);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 根据主键编号查询生产单元类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>ProductionUnitTypeResult</returns>
        [HttpGet("productionunittypes/entity")]
        public async Task<ActionResult> GetProductionUnitTypeEntityById(int id)
        {
            try
            {
                var result = await _productionUnitTypeService.GetProductionUnitTypeAsync(id);
                return Ok(result);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
